package com.example.lgseckill.controller;

import com.example.lgseckill.domain.SeckillProduct;
import com.example.lgseckill.domain.SeckillProductPreheatVO;
import com.example.lgseckill.domain.UserSeckillOrderInfoVO;
import com.example.lgseckill.service.ISeckillOrderService;
import com.example.lgseckill.service.ISeckillProducttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Description: HtmlContorller
 * Author: e
 * CreateDate: 2021-08-02
 */
@Controller
public class HtmlContorller {

    @Autowired
    private ISeckillProducttService seckillProducttService;

    @Autowired
    private ISeckillOrderService seckillOrderService;


    /**
     * 首页
     * @param map
     * @return
     */
    @RequestMapping("/index")
    public String index(ModelMap map,Long userId) {
        List<SeckillProductPreheatVO> seckillProducts = seckillProducttService.getSeckillList();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        seckillProducts.forEach(sp -> {
            sp.setStartTimeStr(format.format(sp.getStartTime()));
            sp.setEndTimeStr(format.format(sp.getEndTime()));
        });
        map.addAttribute("seckillProducts", seckillProducts);
        map.addAttribute("userId", userId);
        return "index";
    }

    /**
     * 订单列表
     * @param map
     * @return
     */
    @RequestMapping("/orderList")
    public String list(ModelMap map,Long seckillOrderId) {
        List<UserSeckillOrderInfoVO> seckillOrderInfo =
                seckillOrderService.findSeckillOrderNowByUserId(seckillOrderId);
        map.addAttribute("seckillOrderInfo",seckillOrderInfo);
        return "order_list";
    }

    /**
     * 订单列表
     * @param map
     * @return
     */
    @RequestMapping("/order_pay")
    public String pay(ModelMap map,Long seckillOrderId,Long userId) {
        map.addAttribute("seckillOrderId",seckillOrderId);
        map.addAttribute("userId",userId);
        return "order_pay";
    }
}
